//HT1622驱动段码屏，COM8 * SEG32
//44 LQFP-A(市场上有盗版的HT1622,需要注意)
//写模式: 占空比 50%, 时钟频率: 4~150KHz, 取 ((150+4)/2)KHz左右的频率
//77KHz,得出H占空比时间:12.9us,取12us,对频率: 83.3KHz
//运行环境: 多任务
//为避免被多任务打断数据传输,数据传输必须在最高级的任务执行


#include "esp32/rom/ets_sys.h"
#include "HT162X.h"
#include "HT162X_Pin.h"
#include "HT162X_Parts_Num.h"
#include "UI.h"


#define HT_Cmd_Sys_ON 0B00000001
#define HT_Cmd_LCD_ON 0B00000011
#define HT_Cmd_LCD_OFF 0B00000010
#define HT_Cmd_RC32K_ON 0B00011000

#define HT_Delay_8us() ets_delay_us(4);

HT162X_Device_S HT_Device;

#define HT_RAM_Length 20
uint8_t HT_RAM[HT_RAM_Length];


void HT_RAM_Setting(Cad_Dat_S Cad_Dat)
{
    if(Cad_Dat.Cad_Dis == Char_ON) HT_RAM[Cad_Dat.Cad_Pin] |= Cad_Dat.Cad_Com;
    else HT_RAM[Cad_Dat.Cad_Pin] &= (~Cad_Dat.Cad_Com);
}

void HT_Write_Begin(void)
{
    HT_Device.HT_CS_Handled(0);
    HT_Delay_8us();
}
void HT_Write_End(void)
{
    HT_Delay_8us();
    HT_Device.HT_CS_Handled(1);
}

//写数据,低4位有效,自低往高写位
void HT162X_W_Bit(uint16_t ht,uint8_t bitNum)
{
    uint8_t i;
    uint16_t temp_Bit;

    if(bitNum == 0) temp_Bit = 1;
    else temp_Bit = (0x1 << (bitNum - 1));
    for (i = 0; i < bitNum; i++)
    {
        HT_Device.HT_Data_Handled(((ht & temp_Bit) == temp_Bit)?1:0);
        HT_Device.HT_WR_Handled(0);
        HT_Delay_8us();
        HT_Device.HT_WR_Handled(1);
        HT_Delay_8us();
        ht <<= 1;
    }
}

void HT162X_Init(void)
{
    HT162X_Pin_Config();
    HT_Device.HT_CS_Handled = HT162X_CS_Handled;
    HT_Device.HT_WR_Handled = HT162X_WR_Handled;
    HT_Device.HT_RD_Handled = HT162X_RD_Handled;
    HT_Device.HT_Data_Handled = HT162X_Data_Handled;

    HT_Write_Begin();
    HT162X_W_Bit(0b100,3); //Write Cmd 100
    HT162X_W_Bit((HT_Cmd_RC32K_ON << 1),9);
    HT162X_W_Bit((HT_Cmd_Sys_ON << 1),9);
    HT162X_W_Bit((HT_Cmd_LCD_ON << 1),9);
    HT_Write_End();

    HT162X_RAM_Reset();
    HT162X_RAM_Tx();
    HT_Delay_8us();
    IO_Lcd_ON_OFF_H;
}

void HT162X_Write(HT_Data_S ht)
{
    HT_Write_Begin();
    HT162X_W_Bit(0b101,3); //Write ID 101
    HT162X_W_Bit(ht.address,6);
    HT162X_W_Bit(ht.data,8);
    HT_Write_End();
}
//Tick 10ms
//必须在最高任务内调用,避免被其他任务打断
void HT162X_RAM_Tx(void)
{
    HT_Data_S wDat;
    uint8_t i;
    uint8_t temp;
    static uint8_t count = 0; //2;
    static uint8_t x = 8;

    if(Factory_Enable > 0)
    {
        count = 0; 
        UI_System.Shine = Shine_Init;
    }
    if(UI_System.Is_Reset != UI_OFF)
    {
        UI_System.Is_Reset = UI_OFF;
        count = 0;
        UI_System.Shine = Shine_All;
    }

    if(x > 0) x--;
    else if(count > 0)
    {
        x = 8;
        count--;
        UI_System.Shine = Shine_All;
        Lcd_NumShow(UI_RTD,888);
    }
    else if(UI_System.Shine == Shine_All) UI_System.Shine = Shine_Init;
    temp = ((HT_RAM_Length + 1) << 1);
    for (i = 0; i < temp; i+=2)
    {
        wDat.address = i;
        if((x > 0) && (x <= 4)) wDat.data = 0x00; // x <= 50
        else wDat.data = HT_RAM[(i >> 1)];
        HT162X_Write(wDat);
    }
}

void HT162X_RAM_Reset(void)
{
    HT_Data_S wDat;
    uint8_t temp;
    uint8_t i;

    for (i = 0; i < HT_RAM_Length; i++)
    {
        HT_RAM[i] = 0x00;
    }
    temp = ((HT_RAM_Length + 1) << 1);
    for (i = 0; i < temp; i+=2)
    {
        wDat.address = i;
        wDat.data = 0x00;
        HT162X_Write(wDat);
    }
}
